//
//  HomeQuestionCompleteInfoTableViewCell.m
//  ZJControlsClass
//
//  Created by mac on 2024/12/2.
//

#import "HomeQuestionCompleteInfoTableViewCell.h"
#import "HomeQuestionShowCompleteInfoTableViewCell.h"
#import "HomeQuestionHeadView.h"

@interface HomeQuestionCompleteInfoTableViewCell ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UIView * follewView;
@property (nonatomic,strong) HomeQuestionHeadView * headView;
@property (nonatomic,strong) UITableView * tableView;
@property (nonatomic,strong) NSMutableArray * dataSource;

@property (nonatomic,strong) UILabel * tureLabel;
@property (nonatomic,strong) UILabel * chooseLabel;
@property (nonatomic,strong) UILabel * msgLabel;


@end




@implementation HomeQuestionCompleteInfoTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self){
        
        self.contentView.backgroundColor = COLOR_RGB(50, 113, 237);
        [self configUI];
        
        
    }
    return self;
}

-(void)configUI{
    
    
//    [self.contentView sd_addSubviews:@[self.follewView,self.tableView,self.tureLabel,self.chooseLabel,self.msgLabel]];
    
    [self.contentView addSubview:self.follewView];
    [self.follewView addSubview:self.tableView];
    [self.follewView addSubview:self.tureLabel];
    [self.follewView addSubview:self.chooseLabel];
    [self.follewView addSubview:self.msgLabel];
    
    
    [self.follewView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).with.offset(ALIGN_WIDTH_PT(12));
        make.right.equalTo(self.contentView).with.offset(-ALIGN_WIDTH_PT(12));
        make.top.equalTo(self.contentView).with.offset(ALIGN_HEIGHT_PT(5));
        make.bottom.equalTo(self.contentView.mas_bottom).with.offset(-ALIGN_HEIGHT_PT(5));
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self.follewView);
        make.height.equalTo(@(ALIGN_HEIGHT_PT(335)));
    }];
    
    [self.tureLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.follewView).with.offset(ALIGN_WIDTH_PT(15));
        make.top.equalTo(self.tableView.mas_bottom).with.offset(ALIGN_HEIGHT_PT(20));
    }];
    
    [self.chooseLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.follewView).with.offset(-ALIGN_WIDTH_PT(15));
        make.top.equalTo(self.tableView.mas_bottom).with.offset(ALIGN_HEIGHT_PT(20));
    }];
    
    [self.msgLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.follewView).with.offset(ALIGN_WIDTH_PT(15));
        make.right.equalTo(self.follewView).with.offset(-ALIGN_WIDTH_PT(15));
        make.top.equalTo(self.tureLabel.mas_bottom).with.offset(ALIGN_HEIGHT_PT(10));
    }];
    
    
}

-(void)setModel:(HomeQuestionModel *)model{
    _model = model;
    self.dataSource = [NSMutableArray arrayWithArray:model.optionsList];
    [self.tableView reloadData];
    
    NSArray * myAnswerArr = [self.model.myAnswer componentsSeparatedByString:@"|"]; // 选择答案 BC
    NSString * myAnswerStr = [myAnswerArr componentsJoinedByString:@" "];
    NSArray * AnswerArr = [self.model.answer componentsSeparatedByString:@"|"]; // 正确答案 AC
    NSString * answerStr = [AnswerArr componentsJoinedByString:@" "];
    
    [self.dataSource enumerateObjectsUsingBlock:^(HomeQuestionOptionsListModel * obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.myAnswer = [NSMutableArray arrayWithArray:myAnswerArr];
        obj.answer = [NSMutableArray arrayWithArray:AnswerArr];
//        obj.answer = [NSMutableArray arrayWithArray:@[@"B",@"C"]];
    }];
    
    NSMutableAttributedString * answerString = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"正确答案：%@",self.model.answer]];
    
    [answerString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, 5)];
    [answerString addAttributes:@{NSForegroundColorAttributeName:COLOR_RGB(50, 113, 237) , NSFontAttributeName:[UIFont systemFontOfSize:16 weight:0.3]} range:NSMakeRange(5, model.answer.length)];
    self.tureLabel.attributedText = answerString;
    
    
    NSMutableAttributedString * myAnswerString = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"已选答案：%@",myAnswerStr]];
    
    [myAnswerString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, 5)];
    
    if ([self.model.myAnswer rangeOfString:self.model.answer].location != NSNotFound){
        [myAnswerString addAttributes:@{NSForegroundColorAttributeName:COLOR_RGB(50, 113, 237) , NSFontAttributeName:[UIFont systemFontOfSize:16 weight:0.3]} range:NSMakeRange(5, myAnswerStr.length)];
    }else{
        [myAnswerString addAttributes:@{NSForegroundColorAttributeName:[UIColor redColor] , NSFontAttributeName:[UIFont systemFontOfSize:16 weight:0.3]} range:NSMakeRange(5, myAnswerStr.length)];
    }
    
    self.chooseLabel.attributedText = myAnswerString;

    
    self.msgLabel.text = [NSString stringWithFormat:@"%@",self.model.explanation ? self.model.explanation : @""];
    
}




#pragma amrk -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HomeQuestionShowCompleteInfoTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"HomeQuestionShowCompleteInfoTableViewCell"];
    if (!cell){
        cell = [[HomeQuestionShowCompleteInfoTableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"HomeQuestionShowCompleteInfoTableViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    HomeQuestionOptionsListModel * model = self.dataSource[indexPath.row];
    
    if ([model.answer containsObject:model.title] && [model.myAnswer containsObject:model.title]){
        model.isTure = YES;
        model.isFalse = YES;
    }else if ([model.answer containsObject:model.title] && ![model.myAnswer containsObject:model.title]){
        model.isTure = YES;
        model.isFalse = NO;
    }else if (![model.answer containsObject:model.title] && [model.myAnswer containsObject:model.title]){
        model.isTure = NO;
        model.isFalse = YES;
    }else{
        model.isTure = NO;
        model.isFalse = NO;
    }
    
    
    if (model.isTure && model.isFalse){
        cell.msgLabel.textColor = COLOR_RGB(50, 113, 237);
        cell.img.image = [UIImage imageNamed:@"对"];
    }else if (model.isTure && !model.isFalse){
        cell.msgLabel.textColor = COLOR_RGB(50, 113, 237);
        cell.img.image = [UIImage imageNamed:@"对"];
    }else if (!model.isTure && model.isFalse){
        cell.msgLabel.textColor = [UIColor redColor];
        cell.img.image = [UIImage imageNamed:@"错"];
    }else{
        cell.msgLabel.textColor = [UIColor blackColor];
        cell.img.image = [UIImage imageNamed:@""];
    }


    cell.model = model;
    
    
    
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    HomeQuestionHeadView * headView = [[HomeQuestionHeadView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - ALIGN_WIDTH_PT(60), 95)];
    headView.backgroundColor = [UIColor whiteColor];
    
    if ([self.model.type intValue] == 1){ // 多选
        headView.typeLabel.text = @"多选题";
    }else{ // 0 是单选
        headView.typeLabel.text = @"单选题";
    }

    headView.msgLabel.text = [NSString stringWithFormat:@"%@.%@",self.model.sort,self.model.stem];
    
    return headView;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return ALIGN_HEIGHT_PT(60);
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return ALIGN_HEIGHT_PT(95);
}


#pragma mark -- 懒加载
-(UIView *)follewView{
    if (!_follewView){
        _follewView = [UIView new];
        _follewView.layer.cornerRadius = 15;
        _follewView.layer.masksToBounds = YES;
        _follewView.backgroundColor = [UIColor whiteColor];
    }
    return _follewView;
}

-(UITableView *)tableView{
    if (!_tableView){
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:(UITableViewStyleGrouped)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.scrollEnabled = NO;
    }
    return  _tableView;
}


-(UILabel *)tureLabel{
    if (!_tureLabel){
        _tureLabel = [UILabel new];
        _tureLabel.textColor = [UIColor blackColor];
        _tureLabel.font = FONT14;
        _tureLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _tureLabel;
}

-(UILabel *)chooseLabel{
    if (!_chooseLabel){
        _chooseLabel = [UILabel new];
        _chooseLabel.textColor = [UIColor blackColor];
        _chooseLabel.font = FONT14;
        _chooseLabel.textAlignment = NSTextAlignmentRight;
    }
    return _chooseLabel;
}



-(UILabel *)msgLabel{
    if (!_msgLabel){
        _msgLabel = [UILabel new];
        _msgLabel.textColor = COLOR_RGB(153, 153, 153);
        _msgLabel.font = FONT14;
        _msgLabel.textAlignment = NSTextAlignmentLeft;
        _msgLabel.numberOfLines = 0;
    }
    return _msgLabel;
}


@end
